﻿<?php
include "./conf/.htconfig.php";
$this_doc = $conf['base_url'] . "administration.php";
$video = $_POST['video'];
if ($_REQUEST['deleteSome'] == 'Delete selected') {
  include "./conf/conn.php";
  $id = $_POST['videoID'];

  foreach ($id as $key => $value) {
    $sql = "DELETE FROM {$conf['db_video_table']} WHERE id='{$value}'";
    $result = mysql_query($sql);
    if (!$result) {
      echo mysql_error();
    }
  }
  mysql_close($con);
}

$video_name = str_replace("_", " ", $video);
include "./conf/conn.php";
$query = "SELECT * FROM " . $conf['db_video_table'] . " WHERE video = '" . $video . "' ORDER BY mark ASC";
$result = mysql_query($query) or die(mysql_error());
mysql_close($con);
$marks = "[";
$first = true;
while ($rad = mysql_fetch_array($result)) {
  $color = null;
  if ($rad['color'] === "Red") {
    $color = "red";
  } else if ($rad['color'] === "Yellow") {
    $color = "yellow";
  } else if ($rad['color'] === "Green") {
    $color = "green";
  }
  $relY = $rad['mark'];
  $mark = "{relY:" . $relY . ",row:1" . $row . ",color:\"" . $color . "\",id:" . $rad['id'] . ",selected:\"no\",time:" . $rad['time'] . "}";
  if (!$first) {
    $marks = $marks . ", " . $mark;
  } else {
    $marks = $marks . $mark;
    $first = false;
  }
}
$marks = $marks . "]";
?>
<!DOCTYPE HTML>
<html>
  <head>
    <title>VAT administration</title>
    <base href="<?= $conf['base_url'] ?>" target="_self" />
    <link rel="stylesheet" type="text/css" href="<?= $conf['base_url'] ?>style/style.css" media="screen" />
    <meta charset="UTF-8">
    <script type="text/javascript" src="<?= $conf['jquerysrc'] ?>"></script>
    <script type="text/javascript" src="<?= $conf['base_url'] ?>js/timeline.js"></script>
    <script type="text/javascript">
      var marks = <?= $marks ?>;
      var rows = 1;

      $(document).ready(function(){
        
        $("#timeline").on("contextmenu",function(event){
          
          var offset = $(this).offset();
          var x = event.pageX - offset.left;
          var y = event.pageY - offset.top;
          clickAction(x, y, 3, marks);
          event.preventDefault();
        }); 
        initTimeline(800, marks, 10);
        printMarks(marks, false);
    
        $("#video").change(function () {
          $("#settings").submit();
        });
	 
        $("#checkAll").click(function()
        {
          var checked_status = this.checked;
          $("input[type='checkbox']").attr('checked', checked_status);
        });
        
        $("#view_timeline").click(function() {
          var image_data = encodeURIComponent($("#timeline")[0].toDataURL("image/png"));
          window.location = "./view_timeline.php?image_data=" + image_data;
        });
      });
    </script>
  </head>
  <body>
    <header>
      <div id="header">Video Annotation Tool</div>
    </header>
    <nav>
      <img src="image/menuarrow.png" alt="meny" id="navimg" />
      <ul id="navmenu">
        <li><a href="index.php" tabindex="1">Hem</a></li>
        <li><a href="administration.php" tabindex="2">Administration</a></li>
        <li><a href="annotate.php?video=<?= $video ?>" tabindex="3">Gör markeringar</a></li>
        <li><a href="view_results.php?video=<?= $video ?>" tabindex="4">Visa makeringar</a></li>
        <li><a href="#" tabindex="5">Hjälp!</a></li>
      </ul>
    </nav>
    <div id="box">
      <form method="post" id="settings" action="<?= $this_doc; ?>">
        <select id="video" name="video" >
          <option value="none">Välj en film</option>
          <?php
          foreach (new DirectoryIterator('./video/') as $fileInfo) {
            if (!$fileInfo->isFile())
              continue;
            $filename = $fileInfo->getFilename();
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            if ($ext === "ogv") {

              $name = pathinfo($filename, PATHINFO_FILENAME);
              $video_name = str_replace("_", " ", $name);
              if ($name === $_POST['video']) {
                echo "<option value='" . $name . "' selected='selected'>" . $video_name . "</option>";
              } else {
                echo "<option value='" . $name . "'>" . $video_name . "</option>";
              }
            }
          }
          ?>
        </select>


        <table>
          <thead>
            <tr>
              <th><input type='checkbox' id="checkAll" /></th>
              <th>Id</th>
              <th>Tid</th>
              <th>Färg</th>
            </tr>
          </thead>
          <tbody>
            <?php
            if ($video != null) {
              include "./conf/conn.php";
              $query = "SELECT * FROM " . $conf['db_video_table'] . " WHERE video = '" . $video . "' ORDER BY sname DESC";
              $result = mysql_query($query) or die(mysql_error());

              while ($row = mysql_fetch_array($result)) {
                echo "<tr><td><input type='checkbox' name='videoID[]' value='" . $row['id'] . "' /></td><td>" . $row['sname'] . "</td><td>" . $row['mark'] . "</td><td>" . $row['color'] . "</td></tr>";
              }
              mysql_close();
            }
            ?>
          </tbody>
        </table>
        <input type="submit" value="Delete selected" name="deleteSome" id="deleteSome" />

      </form>
    </div>
    <div id="container" style="margin-top: 20px; margin-bottom: 10px;">
      <div id="timelineDiv" style="border: 3px solid LightGrey; width: 100%; position: relative; display: block; margin-right: auto; margin-left: auto; margin-top: -3px;">
        <canvas id="timeline" style="z-index: 98; position: absolute; top: 0px; left: 0px;" >
        </canvas>
      </div>
      <button id="view_timeline" type="button">Spara som bild</button>
    </div>
  </body>
</html>